package com.lzs.chuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.chuku.service.PickService;
import com.lzs.chuku.service.PicktaskService;
import com.lzs.entity.*;
import com.lzs.chuku.service.OutorderDetailService;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/chuku/outorder-detail")
public class OutorderDetailController {

    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private PickService pickService;
    @Autowired
    private OutorderDetailService outorderDetailService;

    @PostMapping("addoutorderDetail")
    @ApiOperation("增加出库订单详情")
    public R addoutorderDetail(@RequestBody OutorderDetail outorderDetail){
        return outorderDetailService.add(outorderDetail);
    }

    @PostMapping("findOutordedrdetailById/{id}")
    @ApiOperation("查询出库订单中所有的商品")
    public R findOutordedrdetailById(@PathVariable("id") Long outorderid){
       return outorderDetailService.findAllOUtorderdetailById(outorderid);
    }

    @GetMapping("findCommodityBarByourorderid")
    @ApiOperation("查询所有该出库订单中的所有商品")
    public R findCommodityBarByourorderid(Long outorderid){
        return outorderDetailService.findCommodityBarByourorderid(outorderid);
    }
    @GetMapping("findLocatoByWareId/{id}")
    @ApiOperation("根据仓库id查询出库详情中有的库位")
    public R findLocatoByWareId(@PathVariable("id") Long id){
        try {
            QueryWrapper<OutorderDetail> wrapper = new QueryWrapper();
            wrapper.eq("warehouseid", id);
            List<OutorderDetail> list = outorderDetailService.list(wrapper);
            List<OutorderDetail> list1=new ArrayList();
            for (int i = 0; i <list.size() ; i++) {
                boolean flag=false;
                for (int j = 0; j <list1.size() ; j++) {
                    if (list.get(i).getLocatorid().equals(list1.get(j).getLocatorid())){
                        flag=true;
                        break;
                    }
                }
                if (!flag){
                    list1.add(list.get(i));
                }
            }
            return R.ok().data("list",list1);
        }catch (RuntimeException e){
            return R.error();
        }
    }
    @PostMapping("findCommByLOid")
    @ApiOperation("根据库位查询商品")
    public R findCommByLOid(@RequestBody PickDetail pickDetail){
        try {
            Pick pick = pickService.getById(pickDetail.getPid());
            Picktask picktask = picktaskService.getById(pick.getPicktaskid());
            QueryWrapper<OutorderDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("locatorid", pickDetail.getLocatorid());
            wrapper.eq("pid", picktask.getOutorderid());
            List<OutorderDetail> list = outorderDetailService.list(wrapper);
            return R.ok().data("list",list);
        }catch(RuntimeException e){
            return R.error();
        }
    }

}

